
package shops

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/shops"
    shopsReq "github.com/flipped-aurora/gin-vue-admin/server/model/shops/request"
    "gorm.io/gorm"
)

type LsShopService struct {}
// CreateLsShop 创建lsShop表记录
// Author [yourname](https://github.com/yourname)
func (lsShopService *LsShopService) CreateLsShop(lsShop *shops.LsShop) (err error) {
	err = global.GVA_DB.Create(lsShop).Error
	return err
}

// DeleteLsShop 删除lsShop表记录
// Author [yourname](https://github.com/yourname)
func (lsShopService *LsShopService)DeleteLsShop(ID string,userID uint) (err error) {
	err = global.GVA_DB.Transaction(func(tx *gorm.DB) error {
	    if err := tx.Model(&shops.LsShop{}).Where("id = ?", ID).Update("deleted_by", userID).Error; err != nil {
              return err
        }
        if err = tx.Delete(&shops.LsShop{},"id = ?",ID).Error; err != nil {
              return err
        }
        return nil
	})
	return err
}

// DeleteLsShopByIds 批量删除lsShop表记录
// Author [yourname](https://github.com/yourname)
func (lsShopService *LsShopService)DeleteLsShopByIds(IDs []string,deleted_by uint) (err error) {
	err = global.GVA_DB.Transaction(func(tx *gorm.DB) error {
	    if err := tx.Model(&shops.LsShop{}).Where("id in ?", IDs).Update("deleted_by", deleted_by).Error; err != nil {
            return err
        }
        if err := tx.Where("id in ?", IDs).Delete(&shops.LsShop{}).Error; err != nil {
            return err
        }
        return nil
    })
	return err
}

// UpdateLsShop 更新lsShop表记录
// Author [yourname](https://github.com/yourname)
func (lsShopService *LsShopService)UpdateLsShop(lsShop shops.LsShop) (err error) {
	err = global.GVA_DB.Model(&shops.LsShop{}).Where("id = ?",lsShop.ID).Updates(&lsShop).Error
	return err
}

// GetLsShop 根据ID获取lsShop表记录
// Author [yourname](https://github.com/yourname)
func (lsShopService *LsShopService)GetLsShop(ID string) (lsShop shops.LsShop, err error) {
	err = global.GVA_DB.Where("id = ?", ID).First(&lsShop).Error
	return
}
// GetLsShopInfoList 分页获取lsShop表记录
// Author [yourname](https://github.com/yourname)
func (lsShopService *LsShopService)GetLsShopInfoList(info shopsReq.LsShopSearch) (list []shops.LsShop, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
    // 创建db
	db := global.GVA_DB.Model(&shops.LsShop{})
    var lsShops []shops.LsShop
    // 如果有条件搜索 下方会自动创建搜索语句
    if info.StartCreatedAt !=nil && info.EndCreatedAt !=nil {
     db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
    }
    if info.ShopName != nil && *info.ShopName != "" {
        db = db.Where("shop_name LIKE ?","%"+*info.ShopName+"%")
    }
    if info.ShopTime != nil && *info.ShopTime != "" {
        db = db.Where("shop_time LIKE ?","%"+*info.ShopTime+"%")
    }
    if info.ShopTel != nil && *info.ShopTel != "" {
        db = db.Where("shop_tel = ?",*info.ShopTel)
    }
	err = db.Count(&total).Error
	if err!=nil {
    	return
    }

	if limit != 0 {
       db = db.Limit(limit).Offset(offset)
    }

	err = db.Find(&lsShops).Error
	return  lsShops, total, err
}
func (lsShopService *LsShopService)GetLsShopPublic() {
    // 此方法为获取数据源定义的数据
    // 请自行实现
}
